<?php

namespace App\Service;

use App\depository\RolesDep;
use App\depository\SubPermissionsDep;
use App\depository\SubRolesDep;
use App\depository\SystemDep;
use App\depository\UserDep;
use App\Enum\PermissionsEnum;
use App\Enum\RolesEnum;
use App\Enum\SubCodeEnum;
use App\Enum\TeamsEnum;
use App\Enum\UsersEnum;
use App\depository\TeamsDep;

class DictService
{
    public $dict = [];
    public function __construct(){

    }

    public function enumToDict($enum){
        $res = [];
        foreach ($enum as $index=>$item){
            $res[] = [
                'label' => $item,
                'value' =>$index,
            ];
        }
        return $res;
    }

    public function setTextButton(){

        $this->dict['text_button'] =$this->enumToDict(config('zn_en'));
        return $this;

    }

    public function setAuthorityGroup($teamId){

        $dep = new TeamsDep();
        $resTeam = $dep->first($teamId);
        $group = $resTeam->permission_group;
        if(!$group){
            $this->dict['authority_group'] = [];
            return $this;
        }
        $groupArr = explode("\n",$group);
        foreach ($groupArr as $item){
            $itemArr = explode(":",$item);
            $this->dict['authority_group'][] = [
                "label" => str_replace('Group=','',$itemArr[0]),
                "value" => str_replace('Group=','',$itemArr[0]),
            ];
        }
        return $this;
    }

    public function  setSecretGroup($teamId){
        $dep = new TeamsDep();
        $resTeam = $dep->first($teamId);
        $group = $resTeam->secret_group;
        if(!$group){
            $this->dict['secret_group'] = [];
            return $this;
        }
        $groupArr = explode("\n",$group);
        foreach ($groupArr as $item){
            $itemArr = explode(":",$item);
            $this->dict['secret_group'][] = [
                "label" => str_replace('Group=','',$itemArr[0]),
                "value" => str_replace('Group=','',$itemArr[0]),
            ];
        }
        return $this;
    }

    public function setPermissionType(){
        $this->dict['permission_type'] =$this->enumToDict(PermissionsEnum::$typeArr);
        return $this;
    }

    public function setPermissionStatus(){
        $this->dict['permission_status'] =$this->enumToDict(PermissionsEnum::$statusArr);
        return $this;
    }

    public function setRolesStatus(){
        $this->dict['role_status'] =$this->enumToDict(RolesEnum::$statusArr);
        return $this;
    }

    public function setSubCodeStatus(){
        $this->dict['sub_code_status'] =$this->enumToDict(SubCodeEnum::$statusArr);
        return $this;
    }

    public function setUserStatus(){
        $this->dict['user_status'] =$this->enumToDict(UsersEnum::$statusArr);
        return $this;
    }

    public function setTeamStatus(){
        $this->dict['team_status'] =$this->enumToDict(TeamsEnum::$statusArr);
        return $this;
    }

    public function setRoles(){
        $rolesDep = new RolesDep();
        $resRoles = $rolesDep->allOk();
        $data = $resRoles->map(function($item){
           return [
             'label' => $item->name,
             'value' => $item->id
           ];
        });
        $this->dict['roles'] = $data;
        return $this;
    }

    public function setSubRoles($teamId){

        $rolesDep = new SubRolesDep();
        $resRoles = $rolesDep->allOk($teamId);
        $data = $resRoles->map(function($item){
            return [
                'label' => $item->name,
                'value' => $item->id
            ];
        });
        $this->dict['roles'] = $data;

        return $this;
    }

    public function setDictPermission(){
        $this->dict['dict_permission'] =$this->enumToDict(TeamsEnum::$dictPermissionArr);

    }

    public function setTeams(){
        $teamDep = new TeamsDep();
        $resTeams = $teamDep->allOk();
        $data = $resTeams->map(function($item){
            return [
              'label' => $item->name,
              'value' => $item->id
            ];
        });
        $this->dict['teams'] = $data;
        return $this;
    }



    public function setUsers(){
        $teamDep = new UserDep();
        $resTeams = $teamDep->allOk();
        $data = $resTeams->map(function($item){
            return [
                'label' => $item->name,
                'value' => $item->id
            ];
        });
        $this->dict['users'] = $data;
        return $this;
    }

    public function setPermissions($guard){

        $dep = new SubPermissionsDep();
        $resDetail = $dep->firstByGuardName($guard);

        $dep = new SubPermissionsDep();
        $res = $dep->getByPid($resDetail->id,'button');

        $data = $res->map(function($item){
           return $item->guard_name;
        });

        $this->dict['permissions'] = $data;
        return $this;
    }

    public function setMapJson(){

        $dep = new SystemDep();

        $resSystem = $dep->first();
        $mapJson = json_decode($resSystem->map_json,true);

        $factionArr = $this->enumToDict($mapJson['faction']);
        $armyArr = $this->enumToDict($mapJson['army']);

        $this->dict['faction'] = $factionArr;
        $this->dict['army'] = $armyArr;

        return $this;
    }

    public function getDict(){
        return $this->dict;
    }

}
